//
//  BNDChooseCompanyView.m
//  OfficeDoc
//
//  Created by pie on 2022/3/31.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDChooseCompanyView.h"
#import "UIButton+Convenient.h"
#import "BNDChooseCompanyHelper.h"


@interface BNDChooseCompanyView ()
@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) UIView *tapView;
@property (strong, nonatomic) UITableView *tableView;
@property (assign, nonatomic) CGFloat showViewHight;
@property (strong, nonatomic) BNDChooseCompanyHelper *helper;
@end

@implementation BNDChooseCompanyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor =UIColorHexAndAlpha(0x000000, 0.2);
        _showViewHight = kDeviceHeight*2.0/3.0;
        self.hidden = YES;
        
        [self addSubview:self.tapView];
        [self addSubview:self.contentView];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.textColor = UIColorWithHex(0x34363B);
        titleLabel.font = BNDPFMediumFont(18);
        titleLabel.text = @"切换企业";
        [self.contentView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.contentView);
            make.top.mas_equalTo(self.contentView.mas_top).offset(20);
        }];
        
        [self.contentView addSubview:self.tableView];
        
        [self.helper reload];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self.contentView);
            make.top.mas_equalTo(titleLabel.mas_bottom).offset(20);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-BNDSafeAreaBottomHeight()-70);
        }];
        
        UIButton *clicClosekBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        clicClosekBtn.layer.cornerRadius = 8;
        clicClosekBtn.layer.masksToBounds = YES;
        [clicClosekBtn lk_setTitle:@"取消"];
        [clicClosekBtn lk_setFont:BNDPFRegularFont(17)];
        [clicClosekBtn lk_setTextColor:UIColorWithHex(0x353739)];
        [clicClosekBtn setBackgroundColor:UIColorWithHex(0xF6F7F9)];
        [self.contentView addSubview:clicClosekBtn];
        [clicClosekBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentView.mas_left).offset(32);
            make.right.mas_equalTo(self.contentView.mas_right).offset(-32);
            make.height.mas_equalTo(49);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-BNDSafeAreaBottomHeight()-10);
        }];
        
        @weakify(self)
        [[clicClosekBtn
          rac_signalForControlEvents:UIControlEventTouchUpInside]
         subscribeNext:^(id x) {
            @strongify(self)
            [self hiddenContentView];
        }];
    }
    
    return self;
}

- (UIView*)contentView
{
    if(!_contentView){
        
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(6, kDeviceHeight, kDeviceWidth-12, _showViewHight)];
        _contentView.backgroundColor = UIColor.whiteColor;
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_contentView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(30,30)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _contentView.bounds;
        maskLayer.path = maskPath.CGPath;
        _contentView.layer.mask = maskLayer;
    }
    return _contentView;
}


- (UIView*)tapView
{
    if(!_tapView){
        _tapView = [[UIView alloc] initWithFrame:CGRectMake(6, 0, kDeviceWidth-12,kDeviceHeight-self.showViewHight)];
        _tapView.backgroundColor = UIColor.clearColor;
        @weakify(self)
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(id x) {
            @strongify(self)
            [self hiddenContentView];
        }];
        [_tapView addGestureRecognizer:tap];
    }
    return _tapView;
}

- (UITableView*)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.separatorStyle= UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

- (BNDChooseCompanyHelper*)helper
{
    if(!_helper){
        _helper = [[BNDChooseCompanyHelper alloc] initWith:_tableView];
        @weakify(self)
        [_helper setBlock:^(id _Nullable data) {
            @strongify(self)
            [self hiddenContentView];
            BND_BLOCK_EXEC(self.helperBlock,data);
        }];
    }
    return _helper;
}


- (void)showContentView
{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.frame = CGRectMake(6, kDeviceHeight-self.showViewHight, kDeviceWidth-12, self.showViewHight);
    }];
}

- (void)hiddenContentView
{
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.frame = CGRectMake(6, kDeviceHeight, kDeviceWidth-12, self.showViewHight);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
    
}
@end
